<!--
 * @Description: 会议操作
 * @Author: wind-lc
 * @version: 1.0
 * @Date: 2021-08-18 10:34:47
 * @LastEditTime: 2023-03-13 16:13:12
 * @FilePath: \cscec-pms-admin\src\components\Meeting\MeetingOperation.vue
-->
<template>
  <a-spin :spinning="loading">
    <!-- 会议信息 -->
    <a-card>
      <title-name :title="title !== null ? title : form.meetingName" />
      <custom-form
        ref="form"
        :is-validate="true"
        :edit-model="''"
        :rules="rules"
        :form="form"
        :form-list="formOption"
        :is-emit="false"
      >
        <template
          v-for="item in formOption"
          v-slot:[item.keyName]="scoped"
        >
          <slot
            v-if="item.slot"
            :name="item.keyName"
            v-bind="scoped"
          />
        </template>
      </custom-form>
    </a-card>
    <!-- 附件自定义(如果使用了具名插槽attachment会替换掉原本的附上传，并且这里一定要同时使用自定义附件和自定义提交，避免覆盖原本的附件) -->
    <slot v-if="$scopedSlots.hasOwnProperty('attachment')" name="attachment" :data="form" />
    <!-- 附件自定义 -->
    <!-- 相关资料上传 -->
<!--    :disabled="pageType !== 'enter' && pageType==='view' &&!finalEdit)"-->
    <a-card v-else>
      <title-name title="相关资料上传" />
      <UploadTableNew
        ref="uploadTable"
        :mode="fileOption.mode"
        :init-file-list="fileOption.fileList"
        :need-page="fileOption.needPage"
        :business-code="fileOption.businessCode"
      />
    </a-card>
    <!-- 相关资料上传 -->
    <div class="tool-btns page-btn-right-top">
      <a-button
        v-if="pageType === 'detail' && isCreateUser"
        class="ml20"
        :loading="submitLoading"
        type="primary"
        @click="handleStart"
      >
        开始会议
      </a-button>
      <a-button
        v-if="pageType === 'enter' && isCreateUser"
        class="ml20"
        :loading="submitLoading"
        type="primary"
        @click="handleSubmit(true)"
      >
        提交
      </a-button>
      <a-button v-if="finalEdit" class="ml20" :loading="submitLoading" type="primary" @click="handleSubmit(false)">
        提交
      </a-button>
      <!-- <a-button
        v-if="pageType === 'view'"
        class="ml20"
        :loading="submitLoading"
        type="primary"
      >
        打印
      </a-button> -->
      <a-button
        class="ml20"
        @click="handleClose"
      >
        关闭
      </a-button>
    </div>
  </a-spin>
</template>
<script>
import training from '@/api/design/training'
export default {
  name: 'MeetingOperation' + Date.now(),
  props: {
    // 表单
    form: {
      // 是否作为审批详情
      type: Object,
      default: () => {}
    },
    // 会议类型
    meetingType: {
      type: String,
      require: true
    },
    // 表单配置
    formOption: {
      type: Array,
      default: () => []
    },
    // 校验
    rules: {
      type: Object,
      default: () => {}
    },
    // 页面类型
    pageType: {
      type: String,
      require: true
    },
    // 文件配置
    fileOption: {
      type: Object,
      default: () => {}
    },
    // 自定义提交
    useSubmit: {
      type: Function,
      default: null
    },
    // 自定义附件
    useAttachment:{
      type: Function,
      default: null
    },
    // 标题
    title:{
      type: String,
      default: null
    }
  },
  components: {},
  data() {
    return {
      // 页面加载
      loading: true,
      // 提交加载
      submitLoading: false,
      // 是否创建人
      isCreateUser: false,
      finalEdit: true
    }
  },
  mounted() {
    this.getConferenceDetail()
  },
  methods: {
    /**
     * @description: 获取会议详情
     * @param {void}
     * @return {void}
     */
    async getConferenceDetail() {
      try {
        //触发查询事件用于自定义插槽获取详情
        const { data } = await training.getMeetingById({ id: this.$route.query.id })
        this.meetingById = data
        if(typeof this.useAttachment === 'function'){
          this.useAttachment(this,data)
        }else{
          this.fileOption.fileList = data.uploadList
        }
        this.form = Object.assign(this.form, data)
        this.finalEdit= this.form.meetingStatus == '40' && this.$userInfo().id == this.form.createUserId;
       this.isCreateUser =  this.form?.createUserId === this.$store.state.user.userInfo.id
        this.form.viewName = this.form.branchComName + '/' + this.form.projectName
        this.$emit('getDate',this.form)
        this.loading = false
      } catch (error) {
        console.log(error)
      }
    },
    /**
     * @description: 会议开始
     * @param {void}
     * @return {void}
     */
    handleStart() {
      this.submitLoading = true
      training
        .startMeeting({ meetingId: this.$route.query.id })
        .then(({ code, message }) => {
          if (code === 200) {
            this.$message.success('开始成功')
            this.handleClose()
          } else {
            this.$message.warning(message)
          }
        })
        .finally(() => {
          this.submitLoading = false
        })
    },
    /**
     * @description: 提交
     * @param {void}
     * @return {void}
     */
    handleSubmit(flag) {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if(typeof this.useSubmit === 'function'){
            this.useSubmit(this)
          }else{
            this.submitLoading = true
            training
            .updateByPrimaryKeySelective({
              id: this.$route.query.id,
              meetingType: this.meetingType,
              meetingSummary: this.form.meetingSummary,
              signNum: this.form.signNum,
              projectId: this.form.projectId,
              submitOrUpload: flag,
              uploadList: this.fileOption.fileList.map((el) => {
                const item = this.$clone(el)
                delete item.id
                return item
              })
            })
            .then(({ code, message }) => {
              if (code === 200) {
                this.$message.success('提交成功')
                this.handleClose()
              } else {
                this.$message.warning(message)
              }
            })
            .finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          this.$message.warning("请填写完所有必填项信息")
        }
      })
    },
    /**
     * @description: 关闭
     * @param {void}
     * @return {void}
     */
    handleClose() {
      this.$close()
    }
  }
}
</script>
<style lang="less" scoped>
</style>
